const {
  mock_flag,
  mock_flag_login_type
} = require("../../../data/index.js")
const {
  baseUrl,
  getRequest,
  postRequest
} = require("../../../utils/request.js")
Page({
  data: {
    //账号密码
    username: '',
    password: '',
  },
  /**
   * 跳转
   */
  //首页
  to_home() {
    wx.switchTab({
      url: '/pages/home/index/index',
    })
  },
  //协议
  to_login_contract() {
    wx.navigateTo({
      url: "/pages/user/login/contract/contract",
    })
  },
  /**
   * 账号密码登录
   */
  submitLoginPwd() {
    //判断账号密码是否为空
    if (this.data.username == '' || this.data.password == '') {
      wx.showToast({
        title: '请输入账号密码',
        icon: 'error',
        duration: 2000
      })
      return
    }
    //登录
    if (mock_flag) {
      //保存登录信息
      wx.setStorageSync('isLogin', true);
      //用户类型：1为学生，2为维修工
      if(mock_flag_login_type){
        wx.setStorageSync("userId", 1)
        wx.setStorageSync('userType', 1);
      }else{
        wx.setStorageSync("userId", 7)
        wx.setStorageSync('userType', 2);
      }
      //跳转到首页
      this.to_home()
    }
    if (!mock_flag) {
      this.http_user_loginPassword()
    }

  },
  /**
   * 网络请求
   */
  //用户名密码登录
  http_user_loginPassword() {
    postRequest(baseUrl + "/front/user/loginPassword", {
      username: this.data.username,
      password: this.data.password
    }).then(res => {
      //console.log(res.data)
      if (res.code == 200) {
        //根据token获取登录用户
        this.http_user_getUser(res.data)
      }
    })
  },
  //根据token获取用户
  http_user_getUser(token) {
    getRequest(baseUrl + "/front/user/getUser", {
      token: token
    }).then(res => {
      //console.log(res.data)
      if (res.code == 200) {
        //保存登录信息
        wx.setStorageSync("userId", res.data.userId)
        wx.setStorageSync('isLogin', true);
        wx.setStorageSync('userType', res.data.userType);//用户类型
        //跳转到首页
        this.to_home()
      }
    })
  },
})